package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeBaseDict;
import com.htgd.gardenexpo.page.DictQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.vo.LabelValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/api/dict")
@Api(tags = "字典表")
public class GeBaseDictController {

    @Autowired
    private GeBaseDictService geBaseDictService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeBaseDict geBaseDict) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if(codeExist(geBaseDict.getDictCode())){
                result.setCode(300);
                result.setMessage("code已存在");
            }else {
                geBaseDict.setDelFlag(0);
                geBaseDictService.save(geBaseDict);
                result.setCode(200);
                result.setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }

        return result;
    }

    public boolean codeExist(String code){
        return geBaseDictService.codeExist(code);
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<List<JSONObject>> edit(@RequestBody GeBaseDict geBaseDict) {
        CommonResult<List<JSONObject>> result = new CommonResult<>();
        try {
            geBaseDictService.updateById(geBaseDict);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }


    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeBaseDict geBaseDict = new GeBaseDict();
            geBaseDict.setId(id);
            geBaseDict.setDelFlag(1);
            geBaseDictService.updateById(geBaseDict);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }
    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeBaseDict>> list(DictQuery dictQuery) {
        CommonResult<List<GeBaseDict>> result = new CommonResult<>();
        try {
            PageInfo<GeBaseDict> list = geBaseDictService.pageList(dictQuery);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getItemByCode")
    @ApiOperation("根据code获取字典项")
    public CommonResult<List<LabelValue>> getItemByCode(String code) {
        CommonResult<List<LabelValue>> result = new CommonResult<>();
        try {
            List<LabelValue> list = geBaseDictService.getItemByCode(code);
            result.setCode(200);
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getStatusText")
    @ApiOperation("获取状态文本")
    public String getStatusText(String value) {
       return geBaseDictService.getStatusText(value);
    }

    @GetMapping(value = "/getTopText")
    @ApiOperation("获取置顶文本")
    public String getTopText(String value) {
        return geBaseDictService.getTopText(value);
    }
    @GetMapping(value = "/getTextByCodeAndValue")
    @ApiOperation("获取文本")
    public String getTextByCodeAndValue(String code,String value) {
        return geBaseDictService.getTextByCodeAndValue(code,value);
    }



}
